<?php

/**
 * Index controller class
 *
 */


class IndexController extends Admin_Controller
{
    protected $table_tags;

    public function init()
    {
        parent::init();
        $this->setActiveCat(87);
        $this->table_tags = new Admin_Table_Tags();
    }

    /**
     * index action method
     *
     */
    public function indexAction()
    {
    	$this->page_type = 'index';

        $this->startContent();
	    $this->view->xmlWriter->endElement();
    }
    
    
    public function districtlistAction()
    {
		$id  = intval($_REQUEST['id']);

		$table_cities = new Admin_Table_Auction_Cities();

		$default = '<option value="0">Не задано</option>';

		if($id > 0)
		{
			$items = $table_cities->GetDistrictsList(null, $id);
			if(count($items) > 0)
			{
				echo $default;
				foreach ($items as $k => $v)
					echo '<option value="' . $v['id'] . '">' . $v['name'] .' ('. $v['koatuu'] .')'. '</option>';
			}
			else echo $default;
		}else echo $default;

    	die();
    }


    public function regionlistAction()
    {
    	$id  = intval($_REQUEST['id']);
		$default = '<option value="0">Не задано</option>';

		$table_cities = new Admin_Table_Auction_Cities();

		if($id > 0)
		{
			$items = $table_cities->GetRegionsList(null, $id);
			if(count($items) > 0)
			{
				echo $default;
				foreach ($items as $k => $v)
					echo '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
			}
			else echo $default;
		}else echo $default;

    	die();
    }
    

    public function citylistAction()
    {
		$id  = intval($_REQUEST['id']);
		$id_district  = intval(@$_REQUEST['id_district']);

		$table_cities = new Admin_Table_Auction_Cities();

		$default = '<option value="0">Не задано</option>';

		if($id > 0)
		{
			$items = $table_cities->GetCitiesList(null, null, $id, $id_district);
			if(count($items) > 0)
			{
				echo $default;
				foreach ($items as $k => $v)
					echo '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
			}
			else echo $default;
		}else echo $default;

    	die();
    }    

    public function regionkoatuuAction()
    {
    	$id  = intval($_REQUEST['id']);
		$default = '';

		$table_cities = new Admin_Table_Auction_Cities();

		if($id > 0)
		{
			$items = $table_cities->GetRegionKoatuu($id);
			if(count($items) > 0)
			{
					echo str_pad($items['koatuu'], 10, "0", STR_PAD_LEFT);
			}
			else echo $default;
		}else echo $default;

    	die();
    }

    public function districtkoatuuAction()
    {
    	$id  = intval($_REQUEST['id']);
		$default = '';

		$table_cities = new Admin_Table_Auction_Cities();

		if($id > 0)
		{
			$items = $table_cities->GetDistrictsKoatuu($id);
			if(count($items) > 0)
			{
					echo str_pad($items['koatuu'], 10, "0", STR_PAD_LEFT);
			}
			else echo $default;
		}else echo $default;

    	die();
    }
    
    
    public function catsProductsAction()
    {
    	$id  = intval($_REQUEST['id']);

		if($id > 0)
		{
			$items = $this->table_categories->GetCatList(array(), array('level' => 4, 'is_show' => 1, 'id_menu' => 1, 'parent_id' => $id));
			if(count($items) > 0)
			{
				foreach ($items as $k => $v)
					echo '<option value="' . $v['id'] . '">' . $v['menu_name'] . '</option>';
			}
		}

    	die();
    }

	public function cleanCacheAction()
    {
		$this->_files->SetRootPath($this->_path_real);
		$this->_files->level_access = 2;

    	if(!$this->_files->EmptyFolder('/admin_cache'))  die($this->_files->GetError());
    	if(!$this->_files->EmptyFolder('/client_cache'))  die($this->_files->GetError());

		die('OK');
    }

    // List functions
	public function ajaxCheckboxAction()
    {
		$table_name = $this->_getParam('table_name');
		$id = $this->_getParam('id');
		$field = $this->_getParam('field');
		$val = $this->_getParam('val');

		if($table_name == 'categories' && $field == 'is_active')
		{
			if($val == 0) $this->deactive_cat($id);
			else $this->active_cat($id);
		}
		else
		{
			$where = Zend_Registry::get('db')->quoteInto('id = ?', $id);
    		Zend_Registry::get('db')->update($table_name, array("$field" => $val), $where);
		}

    	die('OK');
    }

    // List functions
	public function ajaxRemoveAction()
    {
		$table_name = $this->_getParam('table_name');
		$id = $this->_getParam('id');

    	$where = Zend_Registry::get('db')->quoteInto('id = ?', $id);
    	Zend_Registry::get('db')->update($table_name, array("is_removed" => 1), $where);

    	die('OK');
    }

    public function searchtagsAction()
    {
    	$tag  = trim($_REQUEST['tag']);
		$data = $this->table_tags->SearchTagsByName($tag);
		if(!(count($data) > 0) && $tag != '')
		{
			$data['nofaund'] = 'Тэг не найден и будет добавлен в базу.';
		}

		echo Zend_Json::encode($data);
    	die();
    }

    private function active_cat($id)
    {
    	$cat = Zend_Registry::get('db')->select()
					->from('categories')
					->where('id = ?', $id)
					->query()
					->fetchAll(Zend_Db::FETCH_ASSOC);

		$where = Zend_Registry::get('db')->quoteInto('id = ?', $id);
		Zend_Registry::get('db')->update('categories', array("is_active" => 1), $where);

		if($cat[0]['parent_id'] > 0) return $this->active_cat($cat[0]['parent_id']);

		return true;
    }

    private function deactive_cat($id)
    {
		$cat = Zend_Registry::get('db')->select()
				->from('categories')
				->where('id = ?', $id)
				->query()
				->fetchAll(Zend_Db::FETCH_ASSOC);

		$where = Zend_Registry::get('db')->quoteInto('l >= ?', $cat[0]['l']);
		$where .= ' and '.Zend_Registry::get('db')->quoteInto('r <= ?', $cat[0]['r']);

		Zend_Registry::get('db')->update('categories', array("is_active" => 0), $where);
    }

    public function tagsAction()
    {
    	$q = $this->_getParam('q');

    	$data = $this->table_tags->SearchTagsByName($q);
    	foreach($data as $k => $v)
    	{
    		echo $v['name']."\n";
    	}
    	die();
    }


}
